package com.ruoyi.web.controller.system.fuw;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.fuw.domain.FuwComm;
import com.ruoyi.fuw.domain.FuwCommsOrders;
import com.ruoyi.fuw.service.IFuwCommService;
import com.ruoyi.fuw.service.IFuwCommsOrdersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwOrder;
import com.ruoyi.fuw.service.IFuwOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/fuw/order")
public class FuwOrderController extends BaseController
{
    @Autowired
    private IFuwOrderService fuwOrderService;

    @Autowired
    private IFuwCommService fuwCommService;

    @Autowired
    private IFuwCommsOrdersService fuwCommsOrdersService;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwOrder fuwOrder)
    {
        startPage();
        List<FuwOrder> list = fuwOrderService.selectFuwOrderList(fuwOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwOrder fuwOrder)
    {
        List<FuwOrder> list = fuwOrderService.selectFuwOrderList(fuwOrder);
        ExcelUtil<FuwOrder> util = new ExcelUtil<FuwOrder>(FuwOrder.class);
        return util.exportExcel(list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return AjaxResult.success(fuwOrderService.selectFuwOrderByOrderId(orderId));
    }

    /**
     * 查看订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:details')")
    @Log(title = "订单详情", businessType = BusinessType.OTHER)
    @GetMapping(value = "/details/{orderId}")
    public AjaxResult details(@PathVariable("orderId") String orderId)
    {
        // 在orders列表中查询该订单信息拥有的商品
        FuwCommsOrders fuwCommsOrders = new FuwCommsOrders();
        fuwCommsOrders.setOrderId(orderId);
        List<FuwCommsOrders> fuwCommsOrdersList = fuwCommsOrdersService.selectFuwCommsOrdersList(fuwCommsOrders);

        // 定义commList存放商品列表信息
        List<FuwComm> fuwCommList = new ArrayList<>();

        // 遍历查询到的orders商品列表信息，去comm商品信息中获取详细的商品信息
        for(FuwCommsOrders fuwCommsOrdersNow: fuwCommsOrdersList){
            FuwComm fuwComm = fuwCommService.selectFuwCommByCommId(fuwCommsOrdersNow.getCommId());
            // 修改商品的折扣，单价 和 将库存修改为购买的数量
            fuwComm.setCommKucun(fuwCommsOrdersNow.getCommNum());
            fuwComm.setCommDanjia(fuwCommsOrdersNow.getCommDanjia());
            fuwComm.setCommZhekou(fuwCommsOrdersNow.getCommZhekou());
            // 将分类优化显示
            String[] types = fuwComm.getCommType().split(",");
            String typeNew = "";
            for(int i=0; i<types.length; i++){
                typeNew = typeNew + types[i];
                if(i < types.length - 1){
                    typeNew = typeNew + " >>> ";
                }
            }
            fuwComm.setCommType(typeNew);
            // 计算每个商品的总金额，然后放入图片列中
            BigDecimal commSul = new BigDecimal(String.valueOf(fuwComm.getCommKucun()));
            BigDecimal commDanjia = new BigDecimal(String.valueOf(fuwComm.getCommDanjia()));
            BigDecimal commZhekou = new BigDecimal(String.valueOf(fuwComm.getCommZhekou()));

            BigDecimal yi = new BigDecimal("1");
            BigDecimal ningDyi = new BigDecimal("0.1");

            // 总金额 = 单价 * 数量 * （1 - 0.1 * 折扣）
            BigDecimal moneyComm = yi.subtract(ningDyi.multiply(commZhekou)).multiply(commSul).multiply(commDanjia);

            fuwComm.setCommPicture(String.valueOf(moneyComm));
            fuwCommList.add(fuwComm);
        }

        return AjaxResult.success(fuwCommList);
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FuwOrder fuwOrder)
    {
        return toAjax(fuwOrderService.insertFuwOrder(fuwOrder));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FuwOrder fuwOrder)
    {
        return toAjax(fuwOrderService.updateFuwOrder(fuwOrder));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:order:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(fuwOrderService.deleteFuwOrderByOrderIds(orderIds));
    }
}
